package com.tms.app.tms.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.google.gson.Gson;
import com.tms.app.tms.app.Constant;
import com.tms.app.tms.bean.UpdateInfo;
import com.tms.app.tms.bean.response.UpdateRes;

import org.greenrobot.eventbus.EventBus;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;

/**
 * Created by Administrator on 2017/11/12 0012.
 */

public class ApkUpdate {


    /***
     * 获取远程app 版本信息
     */
    public static void  getUpdateInfo(){
        RequestParams requestParams = new RequestParams(Constant.UPDATE_INFO);
        requestParams.addBodyParameter("os","0");
        x.http().get(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson=new Gson();
                UpdateRes updateInfo= gson.fromJson(result,UpdateRes.class);
                if (updateInfo.getCode()==200){
                    EventBus.getDefault().post(updateInfo.getData());
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                    ex.printStackTrace();

            }

            @Override
            public void onCancelled(CancelledException cex) {
                cex.printStackTrace();
            }

            @Override
            public void onFinished() {

            }
        });
    }

    /*****
     * 检查本地远程对比状态 ， 0，最新，1，推荐更新2，强制更新
     * @param context
     * @param updateInfo
     * @return
     */
    public static int checkUpdateInfoStatus(Context context,UpdateInfo updateInfo){
        int versionLocal=AppUtil.getVersion(context);

        int versionCode=updateInfo.getVersion_code();
        int versionNoteCode=updateInfo.getNote_code();
        int versionMinCode=updateInfo.getMin_code();
        updateInfo.setActiveCode(3);  //版本太旧、需要强制更新

        if (versionLocal<=versionMinCode){// 比最低运行使用版本低 说明需要强制更新
            updateInfo.setActiveCode(3);
        }
        if (versionMinCode<versionLocal&&versionLocal<=versionNoteCode){// 大于最低版本小于 提醒版本  处于提醒区段
            updateInfo.setActiveCode(2);
        }
        if (versionNoteCode<versionLocal&&versionLocal<versionCode){// 大于提醒，小于最新版本   处于 自主更新区段
            updateInfo.setActiveCode(1);
        }
        if (versionCode<=versionLocal){//   处于 自主最新版本
            updateInfo.setActiveCode(0);
        }

        EventBus.getDefault().post(updateInfo);
        return updateInfo.getActiveCode();
    }


    public static void  InstallApk(Context context,String filePath){
        //   String str = "/CanavaCancel.apk";
        //  String fileName = Environment.getExternalStorageDirectory() + str;
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

    public static void  InstallApk(Context context,File file){
        //   String str = "/CanavaCancel.apk";
        //  String fileName = Environment.getExternalStorageDirectory() + str;
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

    public static  void  UnInstallApk(Context context,String packageName){
        //  Uri packageURI = Uri.parse("package:com.demo.CanavaCancel");
        Uri packageURI = Uri.parse("package:"+packageName);
        Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
        context.startActivity(uninstallIntent);
    }







}
